package cn.edu.mju.cooprateflat;

import cn.edu.mju.cooprateflat.entity._MappingKit;
import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import io.undertow.Undertow;
//https://gitee.com/shaweng/cooprateflat_backend.git
public class FlatConfig extends JFinalConfig {
    public final static Prop prop = PropKit.useFirstFound("config/config_dev_loc.txt","config/config_dev_remote.txt");

    public static void main(String[] args){
        UndertowServer.start(FlatConfig.class, 8081, true);
    }
    @Override
    public void configConstant(Constants constants) {
        constants.setDevMode(true);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.scan("cn.edu.mju.cooprateflat");
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {
        DruidPlugin druidPlugin = createDruidPlugin();
        plugins.add(druidPlugin);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setDialect(new MysqlDialect());
        arp.setShowSql(true);
        plugins.add(arp);
        _MappingKit.mapping(arp);

        RedisPlugin redisPlugin = new RedisPlugin(
                prop.get("redis.cacheName"),
                prop.get("redis.host"),
                prop.getInt("redis.port"),
                prop.getInt("redis.timeout"),
                prop.get("redis.password"),
                prop.getInt("redis.database")
        );
        plugins.add(redisPlugin);
    }

    public static DruidPlugin createDruidPlugin(){
        String jdbcUrl = prop.get("mysql.jdbcUrl");
        String user = prop.get("mysql.user");
        String password = prop.get("mysql.password");
        DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, user, password)
                .setDriverClass("com.mysql.cj.jdbc.Driver")  // 设置数据库的驱动入口类
                .setInitialSize(2)                           // 连接池初始化时的数据库连接数
                .setMinIdle(2)                               // 设置连接池大小的下限
                .setMaxActive(2000)                          // 设置连接池大小的上限
                .setTimeBetweenEvictionRunsMillis(5000)      // 设置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位毫秒
                .setValidationQuery("select 1")              // 设置检测连接是否有效的SQL语句
                .setMinEvictableIdleTimeMillis(3000)         // 最小空闲时间，默认30分钟
                .setFilters("stat,wall");                    // 阿里连接池的过滤器,stat是收集各种信息的,wall是sql防注入
        return druidPlugin;
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {

    }

    @Override
    public void configHandler(Handlers handlers) {

    }
}
